package com.pda.gongpin.ui.emptypalletin;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.gongpin.R;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.request.EmptyShelveRequest;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.EmptyPalletInContract;
import com.pda.gongpin.dialog.AffirmDialog;
import com.pda.gongpin.presenter.EmptyPalletInPresenter;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
@Route(path = AppActivityKey.EMPTYPALLETINACTIVITY)
public class EmptyPalletInActivity extends BaseMvpActivity<EmptyPalletInPresenter> implements EmptyPalletInContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.et_num)
    TextView etNum;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;

    @Override
    public EmptyPalletInPresenter getPresenter() {
        return new EmptyPalletInPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_empty_pallet_in;
    }

    @Override
    public void initView() {
        String storageBin = SharedPreferencesUtil.getInstance().getString(StringKey.STORAGEBIN, "");
        tvPallet.setText(storageBin);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                presenter.scanTheQRCodeToVerify(request);
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvPallet.getText())){
                    showMessage("作业库位不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etNum.getText())){
                    showMessage("托盘不能为空");
                    return;
                }
                AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        EmptyShelveRequest request=new EmptyShelveRequest();
                        request.setPallet(etNum.getText().toString());
                        request.setWorkStorageBinId(tvPallet.getText().toString());
                        presenter.emptyshelve(request);
                    }
                });
                affirmDialog.show("确认要入库吗？");
                break;
        }
    }

    @Override
    public void setScan(ScanResultBean scan) {

        if (scan.getType().equals("B")){
            tvPallet.setText(scan.getCode());
            SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN, scan.getCode());
        }else if (scan.getType().equals("A")){
            etNum.setText(scan.getCode());
        } else {
            showMessage("请扫正确的二维码！");
        }
    }

    @Override
    public void onSuccess() {
        //tvPallet.setText("");
        etNum.setText("");
    }
}
